//下载器
const http = require('http');
const https = require('https');
//
const querystring = require('querystring');
//解析URL地址
const URL = require('url').URL;
const Request = require('./node-request').Request;
const Response = require('./node-response').Response;
module.exports.loadHtml = function(request = new Request(),callback){
    console.log('当前抓取网页:'+request.url);
    let urlp = new URL(request.url);

    if(request.url.startsWith("http://")){
        //get方法
        let queryString = request.url.split('?').filter((str,index)=>index==1).toString();
        let options = {
            hostname: urlp.hostname,
            port: urlp.port,
            path: urlp.pathname+"?"+queryString,
            method: request.method,
            headers: request.headers
        };
        var result = '';
        const req = http.request(options, (res) => {
            res.setEncoding('utf8');
            res.on('data', (chunk) => {
                result+=chunk;
            });
            res.on('end', () => {
                // console.log(`响应头: ${JSON.stringify(res.headers["set-cookie"])}`);
                let resp = new Response({text:result,url:request.url,headers:res.headers});
                callback(resp);
            });
        });

        req.on('error', (e) => {
            console.error(`请求遇到问题: ${e.message}`);
        });
        //post方法
        if(request.method == 'POST'){
            queryString = queryString.stringify(request.parameter);
            req.write(queryString);
        }
        req.end();

    }
    else if(request.url.startsWith("https://")){
        let queryString = request.url.split('?').filter((str,index)=>index==1).toString();
        let options = {
            hostname: urlp.hostname,
            port: 443,
            path: urlp.pathname+"?"+queryString,
            method: request.method,
            headers: request.headers
        };

        var result = '';
        const req = https.request(options, (res) => {
            res.setEncoding('utf8');

            res.on('data', (chunk) => {
                result+=chunk;
            });
            res.on('end', () => {
                let resp = new Response({text:result,url:request.url,headers:res.headers});
                callback(resp);
            });
        });

        req.on('error', (e) => {
            console.error(`请求遇到问题: ${e.message}`);
        });
        //如果为post请求
        if(request.method == 'POST'){
            queryString = queryString.stringify(request.parameter);
            req.write(queryString);
        }
        req.end();
    }
    else{
        throw new Error("url地址错误");
    }
}


